package com.controller.system.staticRole;

import com.entity.system.StaticRole;
import com.service.system.staticRole.StaticRoleService;
import com.util.Const;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/srole")
public class StaticRoleController {

    @Autowired
    StaticRoleService staticRoleService;

    /**
     * 查询角色列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/findRoleList",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> findRoleList(HttpServletRequest request){
        String pageSize = request.getParameter("pageSize");
        String pageNum = request.getParameter("pageNum");
        return staticRoleService.findRoleList(pageNum,pageSize);
    }

    /**
     * 查询所有菜单
     * @param request
     * @return
     */
    @RequestMapping(value = "/findPermissions",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> findPermissions(HttpServletRequest request){
        String roleId = request.getParameter("roleId"); //角色ID
        return staticRoleService.findPermissions(Long.parseLong(roleId));
    }

    /**
     * 修改权限
     * @param request
     * @return
     */
    @RequestMapping(value = "/updatePermissions",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> updatePermissions(HttpServletRequest request){
        String pids = request.getParameter("pids"); //一级菜单ID
        String cids = request.getParameter("cids"); //二级菜单ID
        String qids = request.getParameter("qids"); //查询权限ID
        String aids = request.getParameter("aids"); //添加权限ID
        String dids = request.getParameter("dids"); //删除权限ID
        String eids = request.getParameter("eids"); //修改权限ID
        String roleId = request.getParameter("roleId"); //角色ID
        Map<String,String> map = new HashMap<String, String>();
        map.put("pids",pids);
        map.put("cids",cids);
        map.put("qids",qids);
        map.put("aids",aids);
        map.put("dids",dids);
        map.put("eids",eids);
        map.put("roleId",roleId);
        return staticRoleService.updatePermissions(map);
    }

    /**
     * 查询所有角色和用户的角色
     * @param request
     * @return
     */
    @RequestMapping(value = "/findRoleAndUserRole",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> findRoleAndUserRole(HttpServletRequest request){
        String userId = request.getParameter("userId");
        return staticRoleService.findRoleAndUserRole(userId);
    }

    /**
     * 角色分配
     * @param request
     * @return
     */
    @RequestMapping(value = "/roleDistribution",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> roleDistribution(HttpServletRequest request){
        Map<String,String> map = new HashMap<>();
        map.put("roleId",request.getParameter("roleId"));//角色ID
        map.put("roleName",request.getParameter("roleName"));//角色名称
        map.put("userRoleId",request.getParameter("userRoleId"));//用户当前角色ID
        map.put("userId",request.getParameter("userId"));//用户ID
        map.put("phone",request.getParameter("phone"));//用户手机
        return staticRoleService.roleDistribution(map);
    }

    /**
     * 添加角色
     * @param request
     * @return
     */
    @RequestMapping(value = "/addRole",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> addRole(HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        String roleName = request.getParameter("roleName");
        String roleNote = request.getParameter("roleNote");
        String roleCode = request.getParameter("roleCode");
        if(StringUtils.isEmpty(roleName)){
            map.put("code", Const.ERROR_CODE);
            map.put("message","角色名称不能为空");
            return map;
        }
        if(StringUtils.isEmpty(roleNote)){
            map.put("code", Const.ERROR_CODE);
            map.put("message","角色描述不能为空");
            return map;
        }
        if(StringUtils.isEmpty(roleCode)){
            map.put("code", Const.ERROR_CODE);
            map.put("message","角色CODE不能为空");
            return map;
        }
        map = staticRoleService.addRole(roleName,roleNote,roleCode);
        return map;
    }

    /**
     * 编辑角色
     * @param staticRole
     * @return
     */
    @RequestMapping(value = "/updateRole",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> updateRole(StaticRole staticRole) {
        return staticRoleService.updateRole(staticRole);
    }
}
